﻿using AutoMapper;
using SIG.Infrastructure.Configs;
using SIG.Infrastructure.Helper;
using SIG.InputModel.Front.Shipping;
using SIG.Model.Shipping;
using SIG.Resources.Front;
using SIG.Service;
using SIG.ViewModel;
using SIG.ViewModel.Front.Shipping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SIG.WebMVC.Controllers
{
    public class RecipientInfoController : BaseController
    {
        private readonly IRecipientInfoService _recipientInfoService;
        private readonly IProvinceService _provinceService;
        private readonly ICityService _cityService;
        private readonly IDistrictService _districtService;
        private readonly IMapper _mapper;

        public RecipientInfoController(IRecipientInfoService recipientInfoService, IProvinceService provinceService, 
            ICityService cityService, IDistrictService districtService, IMapper mapper)
        {
            _recipientInfoService = recipientInfoService;
            _provinceService = provinceService;
            _cityService = cityService;
            _districtService = districtService;
            _mapper = mapper;

        }
        // GET: RecipientInfo
        [Authorize]
        public ActionResult Index()
        {
            List<RecipientInfoVM> vm = GetMyAddress();
            return View(vm);
        }

        [Authorize]
        [ChildActionOnly]
        public PartialViewResult AddressList()
        {
            List<RecipientInfoVM> vm = GetMyAddress();
            return PartialView("_AddressList", vm);
        }

        private List<RecipientInfoVM> GetMyAddress()
        {
            var recipientInfos = _recipientInfoService.GetRecipientInfoes(Site.CurrentUserName);
            var vm = _mapper.Map<List<RecipientInfo>, List<RecipientInfoVM>>(recipientInfos);
            return vm;
        }

        [HttpGet]
        public ActionResult Add()
        {
            var vm = new RecipientInfoIM();

            var provinceList = _provinceService.GetAll().OrderBy(c => c.Name).ToList();
            var provinces = new SelectList(provinceList, "Id", "Name");
            ViewBag.Provinces = provinces;

            return PartialView("_Add", vm);
        }



        [HttpPost]
        public JsonResult Add(RecipientInfoIM vm)
        {

            if (!ModelState.IsValid)
            {
                AR.Setfailure(GetModelErrorMessage());
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            var ri = _mapper.Map<RecipientInfo>(vm);
            _recipientInfoService.Create(ri);

            var recipientInfos = _recipientInfoService.GetRecipientInfoes(Site.CurrentUserName);
            var vms = _mapper.Map<List<RecipientInfo>, List<RecipientInfoVM>>(recipientInfos);
            AR.Data = RenderPartialViewToString("_RecipientInfoList", vms);

            AR.SetSuccess(String.Format(Messages.AlertCreateSuccess, Resources.Admin.EntityNames.RecipientInfo));
            return Json(AR, JsonRequestBehavior.DenyGet);

        }


        [HttpGet]
        public ActionResult Edit(int id)
        {

            var  rInfo = _recipientInfoService.GetById(id);
            if (rInfo == null)
            {
                AR.Setfailure(Messages.HttpNotFound);
                return Json(AR, JsonRequestBehavior.AllowGet);
            }

            var vm = _mapper.Map<RecipientInfoIM>(rInfo);

            var provinceList = _provinceService.GetAll().OrderBy(c => c.Name).ToList();
            var provinces = new SelectList(provinceList, "Id", "Name");
            ViewBag.Provinces = provinces;

            var cityList = _cityService.GetAll().Where(m=>m.ProvinceId == vm.ProvinceId).OrderBy(c => c.Name).ToList();
            var cities = new SelectList(cityList, "Id", "Name");
            ViewBag.Cities = cities;

            var districtList = _districtService.GetAll().Where(m=>m.CityId == vm.CityId).OrderBy(c => c.Name).ToList();
            var districts = new SelectList(districtList, "Id", "Name");
            ViewBag.Districts = districts;                     

            return PartialView("_Edit", vm);
        }


        [HttpPost]
        [ValidateAntiForgeryToken]

        public JsonResult Edit(RecipientInfoIM vm)
        {

            if (!ModelState.IsValid)
            {
                AR.Setfailure(GetModelErrorMessage());
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            var rInfo = _mapper.Map<RecipientInfoIM, RecipientInfo>(vm);
            _recipientInfoService.Update(rInfo);


            var recipientInfos = _recipientInfoService.GetRecipientInfoes(Site.CurrentUserName);
            var vms = _mapper.Map<List<RecipientInfo>, List<RecipientInfoVM>>(recipientInfos);
            AR.Data = RenderPartialViewToString("_RecipientInfoList", vms);

            AR.SetSuccess(String.Format(Messages.AlertUpdateSuccess, Resources.Admin.EntityNames.RecipientInfo));
            return Json(AR, JsonRequestBehavior.DenyGet);

        }



        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult IsDefault(int id)
        {
            try
            {
                var emailAccount = _recipientInfoService.SetDefault(id);
                if (emailAccount == null)
                {
                    AR.Setfailure(string.Format( Messages.AlertUpdateFailure, Resources.Admin.EntityNames.RecipientInfo));
                    return Json(AR, JsonRequestBehavior.DenyGet);
                }
                             
                var recipientInfos = _recipientInfoService.GetRecipientInfoes(Site.CurrentUserName);
                var vms = _mapper.Map<List<RecipientInfo>, List<RecipientInfoVM>>(recipientInfos);
                AR.Data = RenderPartialViewToString("_RecipientInfoList", vms);

                AR.SetSuccess(String.Format(Messages.AlertUpdateSuccess, Resources.Admin.EntityNames.RecipientInfo));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            catch (Exception ex)
            {
                AR.Setfailure(ex.Message);
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
        }


        // DELETE: /User/DeleteSite
        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Delete(int id)
        {

            var rinfo = _recipientInfoService.GetById(id);

            if (rinfo==null)
            {
                AR.Setfailure("不存在此地址");
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            _recipientInfoService.Delete(rinfo);
            AR.SetSuccess(String.Format(Messages.AlertDeleteSuccess, Resources.Admin.EntityNames.RecipientInfo));
            return Json(AR, JsonRequestBehavior.DenyGet);

        }


        [HttpGet]
        public PartialViewResult GetCity(int? provinceId)
        {
            List<City> cities = new List<City>();
            if(provinceId==null)
                return PartialView("_CityOptions", cities);

            cities = _cityService.FindBy(m => m.ProvinceId == provinceId).ToList();
            return PartialView("_CityOptions", cities);

        }

        [HttpGet]
        public PartialViewResult GetDistrict(int? cityId)
        {
            List<District> districts = new List<District>();
            if (cityId == null)
                return PartialView("_DistrictOptions", districts);

            districts = _districtService.FindBy(m => m.CityId == cityId).ToList();
            return PartialView("_DistrictOptions", districts);

        }
    }
}